const { app, BrowserWindow } = require("electron");
const path = require("path");

// 屏蔽安全警告
// ectron Security Warning (Insecure Content-Security-Policy)
process.env["ELECTRON_DISABLE_SECURITY_WARNINGS"] = "true";

// 执行createWindow创建窗口
const createWindow = () => {
  const win = new BrowserWindow({
    width: 800, // 窗口宽度
    height: 600, // 窗口高度
    title: "myapp", // 应用标题
    webPreferences: {
      nodeIntegration: true, // 开启node
      contextIsolation: false, // 关闭上下文隔离
      icon: path.join(__dirname, "../public/logo.ico"), // 应用图标
      webSecurity: false,
    },
  });

  // 引入页面
  // development模式
  if (process.env.VITE_DEV_SERVER_URL) {
    win.loadURL(process.env.VITE_DEV_SERVER_URL);
    // 开启控制台
    win.webContents.openDevTools();
  } else {
    win.loadFile(join(__dirname, "./index.html"));
    // 开启控制台
    win.webContents.openDevTools();
  }
};

// Electron 会在初始化后并准备
app.whenReady().then(() => {
  createWindow();
  app.on("activate", () => {
    if (BrowserWindow.getAllWindows().length === 0) createWindow();
  });
});

app.on("request-data", (event) => {
  event.reply("response-data", { key: "value" });
});

app.on("window-all-closed", () => {
  if (process.platform !== "darwin") app.quit();
});
